package org.qpcr.community.project.web;

import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.hswebframework.web.api.crud.entity.PagerResult;
import org.hswebframework.web.api.crud.entity.QueryParamEntity;
import org.hswebframework.web.authorization.annotation.Authorize;
import org.hswebframework.web.authorization.annotation.QueryAction;
import org.hswebframework.web.authorization.annotation.Resource;
import org.hswebframework.web.crud.web.reactive.ReactiveServiceCrudController;
import org.hswebframework.web.exception.NotFoundException;
import org.qpcr.community.project.model.entity.RelateProItemSeqInstanceEntity;
import org.qpcr.community.project.response.RelateProItemSeqDetail;
import org.qpcr.community.project.response.RelateProItemSeqDetailSync;
import org.qpcr.community.project.service.LocalRelateProItemSeqInstanceService;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Mono;

import java.util.List;
import java.util.Map;

/**
 * 产品关联检测项和检测序列
 */
@RestController
@RequestMapping( {"/project/relate-pro-item-seq", "/project/relate/pro/item/seq"})
@Authorize
@Resource(id = "file-center", name = "产品关联检测项和检测序列")
@Slf4j
@Tag(name = "产品关联检测项和检测序列接口")
@RequiredArgsConstructor
public class RelateProItemSeqController implements
    ReactiveServiceCrudController<RelateProItemSeqInstanceEntity, String> {
    @Getter
    private final LocalRelateProItemSeqInstanceService service;

    /**
     * 分页获取详情
     *
     * @param query query
     * @param type  类型 1 检测项 2 检测序列
     * @return Mono<PagerResult<RelateProItemSeqDetail>>
     */
    @PostMapping("/_query/paging-detail/{type:.+}")
    @QueryAction
    @Operation(summary = "分页获取详情")
    public Mono<PagerResult<RelateProItemSeqDetail>> queryRelateProItemSeqDetail(@RequestBody Mono<QueryParamEntity> query,
        @PathVariable Integer type) {
        if (type != 1 && type !=2) {
            return Mono.error(NotFoundException::new);
        }
        return query.flatMap(q -> service
                .queryRelateProItemSeqDetail(q, type)
                .switchIfEmpty(Mono.error(NotFoundException::new)));
    }

    @GetMapping("/query-to-device/{devideId:.+}")
    @QueryAction
    @Operation(summary = "查询未同步设备的产品关联的检测项和检测序列")
    @Authorize(ignore = true)
    public Mono<Map<String, List<RelateProItemSeqDetailSync>>> queryToDevice(@PathVariable String devideId, Long lastUpdateTime) {
        return service.queryToDevice(false, devideId, lastUpdateTime);
    }

    @PostMapping("/sync-to-device/{devideId:.+}")
    @QueryAction
    @Operation(summary = "同步设备的产品关联的检测项和检测序列")
    @Authorize(ignore = true)
    public Mono<Map<String, List<RelateProItemSeqDetailSync>>> syncToDevice(@PathVariable String devideId, Long lastUpdateTime) {
        return service.syncToDevice(devideId, lastUpdateTime);
    }
}
